﻿using System;
using System.Windows.Input;
using MvvmFx.Common.ViewModels;
using MvvmFx.Common.ViewModels.Behaviors.Observable;
using MvvmFx.Common.ViewModels.Commands;

namespace MvvmFx.Samples.HowTos.PropertyRehandlers
{
    public class BoolViewModel : ViewModelBase,
                                 IObservable<BoolViewModel>
    {
        private readonly ActionCommand _setToTrueAction;
        private readonly ActionCommand _setToFalseAction;

        private readonly DelegateCommand _setToTrueDel;
        private readonly DelegateCommand _setToFalseDel;

        private readonly ViewModelCommand _setToTrueVM;
        private readonly ViewModelCommand _setToFalseVM;

        private bool _isTrue;

        public BoolViewModel()
        {
            _setToTrueAction = new ActionCommand(param => IsTrue = true, param => !IsTrue);
            _setToFalseAction = new ActionCommand(param => IsTrue = false, param => IsTrue);

            _setToTrueDel = new DelegateCommand(() => IsTrue = true, () => !IsTrue);
            _setToFalseDel = new DelegateCommand(() => IsTrue = false, () => IsTrue);

            _setToTrueVM = new SetBoolCommand(this, true);
            _setToFalseVM = new SetBoolCommand(this, false);
        }

        /// <summary>
        /// Gets or sets the value of the <see cref=”IsTrue”/> property.
        /// </summary>
        public bool IsTrue
        {
            get { return _isTrue; }
            set
            {
                if (_isTrue != value)
                {
                    _isTrue = value;
                    OnPropertyChanged("IsTrue");
                }
            }
        }

        /// <summary>
        /// Gets the value of the <see cref=”SetToTrueAction”/> property.
        /// </summary>
        public ActionCommand SetToTrueAction
        {
            get { return _setToTrueAction; }
        }

        /// <summary>
        /// Gets the value of the <see cref=”SetToFalseAction”/> property.
        /// </summary>
        public ActionCommand SetToFalseAction
        {
            get { return _setToFalseAction; }
        }

        /// <summary>
        /// Gets the value of the <see cref=”SetToTrueDel”/> property.
        /// </summary>
        public DelegateCommand SetToTrueDel
        {
            get { return _setToTrueDel; }
        }

        /// <summary>
        /// Gets the value of the <see cref=”_setToFalseDel”/> property.
        /// </summary>
        public DelegateCommand SetToFalseDel
        {
            get { return _setToFalseDel; }
        }

        /// <summary>
        /// Gets the value of the <see cref=”SetToTrueVM”/> property.
        /// </summary>
        public ViewModelCommand SetToTrueVM
        {
            get { return _setToTrueVM; }
        }

        /// <summary>
        /// Gets the value of the <see cref=”SetToFalseVM”/> property.
        /// </summary>
        public ViewModelCommand SetToFalseVM
        {
            get { return _setToFalseVM; }
        }

        #region IObservable<BoolViewModel> Members

        public PropertyObserver<BoolViewModel> NewPropertyObserver()
        {
            return new PropertyObserver<BoolViewModel>(this);
        }

        #endregion
    }
}